<template>
  <div style="display: flex">
    <el-input placeholder="文章标题" v-model="query.headline"/>
    <el-button :loading="loading" type="primary" @click="findByHeadline">查询题目</el-button>
  </div>
  <div class="essay-item" v-for="item in articleLst" :key="item.id" @click="showDetail(item.id)">
    <div style="margin-right: 5px;">
      <el-image v-if="item.coverUrl" :src="item.coverUrl" fit="scale-down" style="height: 120px;"/>
    </div>
    <div style="width: 100%;">
      <div><router-link :to="`/problem/detail?id=${item.id}`"><h1>{{ item.title }}</h1></router-link></div>
<!--      <div style="margin: 20px 0">-->
<!--        <router-link class="summarize" :to="`/article?articleId=${item.id}`">-->
<!--          <div>{{ item.summarize }}</div>-->
<!--        </router-link>-->
<!--      </div>-->
<!--      <div style="display: flex; justify-content: space-between; padding-left: 10px; align-content: space-between">-->
<!--        <div style="display:flex; width: 270px; justify-content: space-between">-->
<!--          <div>-->
<!--            <el-button size="small">点赞 {{ item.likeNo }}</el-button>-->
<!--            <el-button size="small" type="danger">已赞 {{ item.likeNo }}</el-button>-->
<!--          </div>-->
<!--          <div>-->
<!--            <el-button size="small">踩</el-button>-->
<!--            <el-button size="small" type="info">已踩</el-button>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div>-->
<!--          <el-button size="small" link>作者：{{ item.authorId }}</el-button>-->
<!--          <el-button v-if="item.releaseTime" size="small" link>发布时间：{{ item.releaseTime.replace('-', '年').replace('-', '月').replace(' ', '日 ') }}</el-button>-->
<!--          <el-button size="small" link>浏览数：{{ item.viewsNo }}</el-button>-->
<!--        </div>-->
<!--      </div>-->
    </div>
  </div>
  <div class="pagination-block">
    <el-pagination
      v-model:current-page="page.current"
      v-model:page-size="page.size"
      :page-sizes="[5, 10, 20, 30, 40]"
      :small="false"
      :disabled="false"
      :background="false"
      layout="total, sizes, prev, pager, next, jumper"
      v-model:total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { ref } from 'vue'
import { articleByHeadline, articleRecommend } from '@/api/article'
import { problemList } from '@/api/problem'

export default {
  name: 'problem-list',
  setup () {
    return {
      articleLst: ref([]),
      page: ref({
        current: 1,
        size: 5,
        total: 0
      }),
      query: ref({
        headline: ''
      }),
      loading: ref(false)
    }
  },
  methods: {
    findByHeadline () {
      this.loading = true
      if (this.query.headline) {
        articleByHeadline(this.query.headline).then(value => {
          this.articleLst = value.data.records
          this.page.size = value.data.size
          this.page.current = value.data.current
          this.page.total = value.data.total
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.loading = false
        this.getProblemList()
      }
    },
    showDetail (articleId) {
      // this.$router.push('/article?articleId=' + articleId)
    },
    getProblemList (page, query) {
      problemList({ ...query, ...page }).then(value => {
        console.log('haha', value)
        this.articleLst = value.data.records
        this.page.size = value.data.size
        this.page.current = value.data.current
        this.page.total = value.data.total
      })
    },
    handleSizeChange (val) {
      console.log(val)
      console.log('page', this.page)
      this.getProblemList(this.page, this.query)
    },
    handleCurrentChange (val) {
      console.log(val)
      this.getProblemList(this.page, this.query)
    }
  },
  mounted () {
    this.getProblemList(this.page, this.query)
  }
}
</script>

<style scoped>
.essay-item{
  margin: 20px 0px;
  padding: 10px;
  background-color: #fcfcfc;
  display: flex;
}
.essay-item:hover {
  background-color: #e3eeef;
}
.essay-item > div >div{
  display: flex;
}

.essay-item > div{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.pagination-block{
  background-color: #fdfdfe;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  margin-top: 10px;
  box-shadow: rgba(0, 0, 0, 0.25) 0px 14px 28px, rgba(0, 0, 0, 0.22) 0px 10px 10px;
}
.pagination-block .demonstration {
  margin-bottom: 16px;
}

.essay-item a{
  color: #586c88;
  text-decoration: none;
}
.essay-item h1:hover{
  text-decoration: underline 1px slategray solid;
}
.essay-item a:hover{
  text-decoration: none;
  color: #2c3e50;
}

</style>
